package Air_war;

import java.awt.image.BufferedImage;
import java.util.Random;
//敌机类
public abstract class FlyingObject {
    //活着  死了  删除的   当前状态
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    public static final int REMOVE = 2;
    protected int state = LIVE;
    //受保护的   宽高整合
    protected int width;
    protected int height;
    protected int x;
    protected int y;
    //敌机组合
    public FlyingObject(int width,int height){
        this.width = width;
        this.height = height;
        Random rand = new Random();
        x = rand.nextInt(World.WIDTH-width);
        y = -height;
    }
    //英雄机  天空  子弹
    public FlyingObject(int width,int height,int x,int y){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }
    //移动
    public abstract void step();
    //获取对象的图片
    public abstract BufferedImage getImage();
    //判断是否是活着的
    public boolean isLive(){return state==LIVE;}//若状态为LIVE则为true，表示或者的，若不是则返回false
    public boolean isDead(){return state==DEAD;}
    public boolean isRemove(){return state==REMOVE;}
    //判断敌人是否越界
    public boolean isOutOfbounds(){return y>=World.HEIGHT;}//敌人的y>=窗口的高   既为越界了

    //检测碰撞       this:敌人      other:子弹/英雄机
    public boolean isHit(FlyingObject other){
        int x1 = this.x-other.width;//x1:小敌机的x-英雄机的宽
        int x2 = this.x+this.width;//x2:小敌机的x+英雄机的宽
        int y1 = this.y-other.height;//y1:小敌机的x-英雄机的高
        int y2 = this.y+this.height;//y2:小敌机的x-英雄机的高
        int x= other.x;//x:英雄机的x
        int y = other.y;//英雄机的y

        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
        //x在x与x2之间,并且y在y1与y2之间,表示撞上了
    }

    public void goDead(){
        state = DEAD;//将当前状态改为DEAD   死了的
    }








}
